用两个栈实现队列 Posted on 2019-08-23 | In 剑指offer | | reads times 用两个栈实现队列题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 代码 1234567891011121314151617const inStack = []const outStack = []//123456//入栈【123456】先出6,先入1//入队【123456】先出1,后出6//用栈模拟队列,那么出队列相当于用入的栈一个一个出栈进入出的栈,function push(node) { inStack.push(node)}function pop() { if(!outStack.length){ while(inStack.length){ outStack.push(inStack.pop()) } } return outStack.pop()} Post author: GoldMiner Xun Post link: https://goldminerxun.github.io/2019/08/23/%E5%89%91%E6%8C%87offer%20JavaScript%E7%89%88%20(5)/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.